-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve iPython interoperability #7268
Conversation
for more information, see https://pre-commit.ci
This conversation should be re-evaluated if/when someone from the IPython team responds, but for the moment, I'm not really in favour of the various automatic changes you're making here. In particular, you've added scaling for I;16 images, which is a change regarding #3159 that would occur here, but nowhere else. For an analogous situation, in #7241, you were concerned about the speed of saving images for IPython. I was in favour of #7242 as a solution because it mirrored the behaviour of ImageShow. I think any arguments you make here for IPython would similarly apply to ImageShow. Different users might have different preferences for how much they want images to be scaled down by. I know you've added a constant to try and make this configurable - my feeling is just that it is better to let the user specify what they would like, rather than guessing. If I found images displaying at a small size, I would first believe that the image itself was small, rather than expecting a library to be transforming it automatically. Those are just my thoughts though. You're welcome to leave this open, and if someone else approves of this plan, I raise no objections. |
Mostly did this so I could have a current version that installed locally that behaved better with Jupyter. Happy to leave it open until some decision can be reached. Re, the Image.fromarray(np.random.randint(65536, size=(100, 100), dtype='uint16')).save('out.jp2')
print(im.open('out.jp2').mode) gives me I've made it print warnings so user has some indication of when it's making non-trivial changes. But I agree that I've baked lot of my preferences into policy rather than providing the mechanisms and letting the user define their preferred policy. I've been reading more of their API, and have seen that you can use from IPython.display import display
def show_image(obj, *args, **kwargs):
display({
'text/plain': f"custom={str(obj)}",
# 'image/png': b'...',
# 'image/jpeg': b'...',
}, raw=True)
get_ipython().display_formatter.ipython_display_formatter.for_type(Image.Image, show_image)
Image.new('L', (10, 10)) and have some method available that has some common policy, but could be swapped out with another formatter as required. The issue is that this seems to be even more entangled in the guts of IPython. |
@n3011 Any opinion here? |
Expose Image.use_display_hook_features that can be used to control how images are seen in IPython / Jupyter. For example: Image.use_display_hook_features(F_mode='unit') special cases F mode images, by assuming values are in [0., 1.]. Alternatively, the user could do: Image.use_display_hook_features('auto') and it will function similarly to my previous patch.
Following all the activity on other related issues, I've had a go at reworking this change. By default it will now only return either a PNG or JPEG image, as determined by whichever encodes to less bytes. It won't do any other conversion. I've added a Image.use_display_hook_features(F_mode='unit', mode_map='auto') would allow the following images to be displayed automatically: Image.new('HSV', (400, 400))
Image.fromarray(np.random.random((400, 400))) Or you can mostly "full auto" mode with: Image.use_display_hook_features('auto', F_mode='unit') which also resizes large images and other things. Interested to hear opinions! |
It's a quite big change and complicated API, especially when I still don't see any explanation why |
A comment from the author of this PR - #7259 (comment)
As I've indicated earlier, I think this is somewhat too... automatic. This PR is trying to solve #3159, #2663, and also scaling images automatically, but only for IPython, while against an IPython decision. It's trying to more IPython use easier, but the boundaries for this change are irregularly defined enough that I think this should be code on the user's end, rather than something built into Pillow. |
Fixes #7259
Changes proposed in this pull request:
_repr_mimebundle_
by returning either PNG or JPEGmode
where possible_repr_png|jpeg_
to do the same pre-processingcompression_level=1
I've been finding the current behavior to be unusable in a Jupyter notebook. The number of dumped backtraces are very painful to work with, so I've tidied up my method in #7259 and incorporated it into the codebase. Until I get a response to my question from the IPython project I think this code does reasonable things.